package com.yingma.common.controller;

import com.yingma.common.util.AjaxResult;
import com.yingma.system.entity.JcZone;
import com.yingma.system.pojo.TreeSelect;
import com.yingma.system.service.JcZoneService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * (JcZone)表控制层
 *
 * @author makejava
 * @since 2020-03-22 12:24:01
 */
@RestController
@RequestMapping("jcZone")
public class JcZoneController {
    /**
     * 服务对象
     */
    @Resource
    private JcZoneService jcZoneService;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public JcZone selectOne(String id) {
        return this.jcZoneService.queryById(id);
    }

    /**
     * 根据 编码或者名称 进行分页查询省级区域信息
      * @param code
     * @param Name
     * @param pageNum
     * @param pageSize
     * @return
     */
    @ApiOperation("查询省级区域信息")
    @RequestMapping(value = "ProvinceZoneInfo",method = RequestMethod.POST)
        public AjaxResult SearchProvinceInfo(@ApiParam("编码") String code, @ApiParam("城市名称") String Name,@ApiParam("区域ID") String zoneid, @ApiParam("当前页数") Integer pageNum, @ApiParam("每页数据量") Integer pageSize){
        List<JcZone> list=new ArrayList<JcZone>();
        if(!zoneid.equals("")){
            //根据用户点击左边的树状菜单来获取信息
            list=jcZoneService.selectZoneById(zoneid, pageNum, pageSize);
        }else {
            //获取省级的区域信息
            list= jcZoneService.getProviceInfo(code, Name, pageNum, pageSize);
        }

        AjaxResult ajaxResult=AjaxResult.success(list);
        System.out.println("ajaxResult 的数据是=>"+ajaxResult.toString());
        return AjaxResult.success(list);
    }

    /**
     * 获取区域下拉树列表
     */
    @PostMapping("/treeselect")
    public AjaxResult treeselect(JcZone jcZone)
    {
        List<TreeSelect> list=null;
        List<JcZone> jcZones = jcZoneService.selectZoneList(jcZone);
        //System.out.println(jcZone);
        //
        ValueOperations<String, Object> operations = redisTemplate.opsForValue();
         if(redisTemplate.hasKey("zonetreelist")){
             //如果缓存中有就去缓存中拿数据
             list=(List<TreeSelect>)operations.get("zonetreelist");
         }else {
             list=jcZoneService.buildZoneTreeSelect(jcZones);
             operations.set("zonetreelist",list);
         }

        System.out.println(list);
        System.out.println(list.size());
        return AjaxResult.success(list);
    }

}
